<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>轮播图点击切换</title>
  <style>
    * {
      box-sizing: border-box;
    }

    .slider {
      width: 560px;
      height: 660px;
      overflow: hidden;
    }

    .slider-wrapper {
      width: 100%;
      height: 560px;
    }

    .slider-wrapper img {
      width: 100%;
      height: 100%;
      display: block;
    }

    .slider-footer {
      height: 80px;
      background-color: rgb(100, 67, 68);
      padding: 12px 12px 0 12px;
      position: relative;
    }

    .slider-footer .toggle {
      position: absolute;
      display: flex;
      right: 0;
      top: 12px;
    }

    .slider-footer .toggle button {
      margin-right: 12px;
      width: 28px;
      height: 28px;
      border: none;
      appearance: none;
      color: #fff;
      background-color: rgba(255, 255, 255, 0.1);
      border-radius: 4px;
      cursor: pointer;
    }

    .slider-footer .toggle button:hover {
      background-color: rgba(255, 255, 255, 0.2);
    }

    .slider-footer p {
      margin: 0;
      color: #fff;
      font-size: 18px;
      margin-bottom: 10px;
    }

    .slider-indicator {
      margin: 0;
      padding: 0;
      display: flex;
      list-style: none;
      align-items: center;
    }

    .slider-indicator li {
      width: 8px;
      height: 8px;
      margin: 4px;
      border-radius: 50%;
      background: #fff;
      opacity: 0.4;
      cursor: pointer;
    }

    .slider-indicator li.active {
      width: 12px;
      height: 12px;
      opacity: 1;
    }
  </style>
</head>

<body>
  <div class="slider">
    <div class="slider-wrapper">
      <img src="../pic/1.jpg" alt="">
    </div>
    <div class="slider-footer">
      <p>王菲</p>
      <ul class="slider-indicator">
        <li class="active"></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
      </ul>
      <div class="toggle">
        <button class="prev">&lt;</button>
        <button class="next">&gt;</button>
      </div>
    </div>
  </div>
  <script>
    // 1.初始数据
    const sliderData = [    //注意下标是0-7
      { url: '../pic/1.jpg', title: '王菲', color: 'rgb(100, 67, 68)' },    //下标为0
      { url: '../pic/2.jpg', title: '陈奕迅', color: 'rgb(43, 35, 26)' },   //下标为1
      { url: '../pic/3.jpg', title: '李克勤', color: 'rgb(36, 31, 26)' },   //2
      { url: '../pic/4.jpg', title: '张学友', color: 'rgb(139, 98, 66)' },  //3
      { url: '../pic/5.jpg', title: '林俊杰', color: 'rgb(67, 90, 92)' },   //4
      { url: '../pic/6.jpg', title: '周杰伦', color: 'rgb(166, 131, 143)' },//5
      { url: '../pic/7.jpg', title: '刘德华', color: 'rgb(53, 29, 25)' },   //6
      { url: '../pic/8.jpg', title: '周柏豪', color: 'rgb(99, 72, 114)' },  //7
    ]

    // 1.获取元素
    const img = document.querySelector('.slider-wrapper img')
    const p = document.querySelector('.slider-footer p')

    let i = 0  //（非常重要）信号量 控制图片的张数
    // 2.开启定时器
    setInterval(function () {
      i++
      if (i >= sliderData.length) { i = 0 }   //图片播放到最后一张，就是大于等于数组的长度
      console.log(`${i + 1}`)
      // console.log(sliderData[i]) //1,2,3,4,5,6,7,0,1,2......
      // 更换图片路径
      img.src = sliderData[i].url
      // 把字写到p里面
      p.innerHTML = sliderData[i].title
      // 小圆点
      // 先删除以前的active
      document.querySelector('.slider-indicator .active').classList.remove('active')
      // document.querySelector(`.slider-indicator li:nth-child(${i})`).classList.remove('active')
      // 只让当前的li添加active
      document.querySelector(`.slider-indicator li:nth-child(${i + 1})`).classList.add('active')
    }, 1500)

  </script>
</body>

</html>